/*
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.android.sync;

import org.geoscheduler.android.GSContext;
import org.geoscheduler.android.R;
import org.geoscheduler.android.activities.ListEvents;
import org.geoscheduler.android.activities.ShowLongMessage;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;

/**
 * FIXME [srakyi] comment me!
 *
 * @author srakyi
 */
public class SyncEventsService extends Service {

	private static final String LOGTAG = "SyncEventsService";

	private Location currentLocation;

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	/**
	 * Object receiving interaction from client 
	 */
	private final IBinder mBinder = new Binder() {
		@Override
		protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
			return super.onTransact(code, data, reply, flags);
		}
	};

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		Log.d(LOGTAG, LOGTAG + ": activity started");

		currentLocation = (Location) intent.getExtras().get("location");
		Log.d(LOGTAG, LOGTAG + ": location (" 
				+ new Double(currentLocation.getLatitude() * 1E6).intValue()
				+ ","
				+ new Double(currentLocation.getLongitude() * 1E6).intValue()
				+ ")");

		Thread thr = new Thread(null, mTask, this.getClass().getSimpleName());
		thr.start();
	}

	Runnable mTask = new Runnable() {
		public void run() {
			synchronized (mBinder) {
				GSContext.loadPrefs(SyncEventsService.this);
				
				// app intent
				Intent appIntent = new Intent(SyncEventsService.this, ListEvents.class);

				NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

				// Note: R.drawable.sync_failed is used in both notifications as unique notification id, not only as icon id
				try {
					Intent notifIntent = null;
					
					GSContext.getEventSynchronizer(SyncEventsService.this).updateTemporaryEvents(new Double(currentLocation.getLatitude() * 1E6).intValue(),
							new Double(currentLocation.getLongitude() * 1E6).intValue());

					// FIXME and what about notifIntent??
					Notification notif = new Notification(R.drawable.sync_ok,
							getText(R.string.syncev_msg_sync_succ),
							System.currentTimeMillis());
					notif.setLatestEventInfo(SyncEventsService.this, 
							getText(R.string.syncev_msg_sync_succ),
							getText(R.string.syncev_msg_sync_succ_detail), 
							PendingIntent.getActivity(SyncEventsService.this, R.drawable.sync_ok, appIntent, 0));
					notificationManager.notify(R.drawable.sync_failed, notif);
//					notificationManager.notify(R.drawable.sync_failed, new Notification(SyncEventsService.this, R.drawable.sync_ok,
//							getText(R.string.syncev_msg_sync_succ), System.currentTimeMillis(), getText(R.string.syncev_msg_sync_succ),
//							getText(R.string.syncev_msg_sync_succ_detail), notifIntent, R.drawable.icon, getText(R.string.app_name), appIntent));
					Log.d(LOGTAG, LOGTAG + ": sync succesfull");
				} catch (Exception e) {
					Intent notifIntent = new Intent(SyncEventsService.this, ShowLongMessage.class); 
			 	 	notifIntent.putExtra(ShowLongMessage.class.getName() + ShowLongMessage.PAR_TITLE, getText(R.string.syncev_label_last_failure)); 
			 	 	notifIntent.putExtra(ShowLongMessage.class.getName() + ShowLongMessage.PAR_MSG, e.getMessage());
			 	 	
			 	 	// FIXME and what about notifIntent??
			 	 	Notification notif = new Notification(R.drawable.sync_failed,
							getText(R.string.syncev_msg_sync_failed),
							System.currentTimeMillis());
					notif.setLatestEventInfo(SyncEventsService.this, 
							getText(R.string.syncev_msg_sync_failed),
							getText(R.string.syncev_msg_sync_failed_detail), 
							PendingIntent.getActivity(SyncEventsService.this, R.drawable.sync_failed, appIntent, 0));
					notificationManager.notify(R.drawable.sync_failed, notif);
//			 	 	notificationManager.notify(R.drawable.sync_failed, new Notification(SyncEventsService.this, R.drawable.sync_failed,
//							getText(R.string.syncev_msg_sync_failed), System.currentTimeMillis(), getText(R.string.syncev_msg_sync_failed),
//							getText(R.string.syncev_msg_sync_failed_detail), notifIntent, R.drawable.icon, getText(R.string.app_name), appIntent));
					Log.d(LOGTAG, LOGTAG + ": sync failed");
				}
			}

			SyncEventsService.this.stopSelf();
		}
	};

}